package com.sunny.ynhz.model;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.RiverChiefBeanMsg;
import com.sunny.ynhz.bean.RiverDutyBean;
import com.sunny.ynhz.utils.GsonUtils;
import com.sunny.ynhz.utils.SPUtil;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by huhuan123 on 2018/9/16.
 *
 * @describe TODO
 */

public class RiverChiefModel {
    Context context;
    IProblemTypeResultView iProblemTypeResultView;

    public RiverChiefModel(Context context, IProblemTypeResultView iProblemTypeResultView) {
        this.context = context;
        this.iProblemTypeResultView = iProblemTypeResultView;
    }

    @SuppressWarnings("unchecked")
    public void getRiverChiefData(int type, boolean isCache) {
        String userId = SPUtil.get("userId", "").toString();
        RiverChiefBeanMsg riverChief = GsonUtils.getInstance("riverChief", RiverChiefBeanMsg.class);
        if (isCache && riverChief != null) {
            iProblemTypeResultView.onSuccess(riverChief);
            return;
        }
        HttpManager.getInstance().getRiverChiefBean(userId, type).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(context, new ApiSubscriber.HttpOnResultListener<RiverChiefBeanMsg>() {
            @Override
            public void onNext(RiverChiefBeanMsg riverChiefBean) {
                GsonUtils.putString("riverChief", riverChiefBean);
                if (isCache) {
                    iProblemTypeResultView.onSuccess(riverChiefBean);
                }

            }

            @Override
            public void onCompleted() {
                if (isCache) {

                }
            }

            @Override
            public void onstart() {
                if (isCache) {
                    iProblemTypeResultView.onStartLoad();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (isCache) {
                    iProblemTypeResultView.onFail();
                }
            }
        }));
    }

    @SuppressWarnings("unchecked")
    public void getRiverDutyData(int type, boolean isCache) {
        String userId = SPUtil.get("userId", "").toString();
        RiverDutyBean riverChief = GsonUtils.getInstance("riverDuty", RiverDutyBean.class);
        if (isCache && riverChief != null) {
            iProblemTypeResultView.onSuccess(riverChief);
            return;
        }
        HttpManager.getInstance().getRiverDutyBean(userId, type).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(context, new ApiSubscriber.HttpOnResultListener<RiverDutyBean>() {
            @Override
            public void onNext(RiverDutyBean riverChiefBean) {
                GsonUtils.putString("riverDuty", riverChiefBean);
                if (isCache) {
                    iProblemTypeResultView.onSuccess(riverChiefBean);
                }

            }

            @Override
            public void onCompleted() {
                if (isCache) {

                }
            }

            @Override
            public void onstart() {
                if (isCache) {
                    iProblemTypeResultView.onStartLoad();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (isCache) {
                    iProblemTypeResultView.onFail();
                }
            }
        }));
    }

    public List<RiverChiefBeanMsg.DateBean> serachPersion(List<RiverChiefBeanMsg.DateBean> mList, String name, String codeStr) {
        List<RiverChiefBeanMsg.DateBean> mListRiverChief = new ArrayList<>();
        if (TextUtils.isEmpty(codeStr) && TextUtils.isEmpty(name)) {
            mListRiverChief.addAll(mList);
            return mListRiverChief;
        }
        for (int i = 0; i < mList.size(); i++) {
            RiverChiefBeanMsg.DateBean dateBean = mList.get(i);
            String nameStr = dateBean.getNAME();
            String addvcdarea = dateBean.getADDVCDAREA();
            if (TextUtils.isEmpty(codeStr) && !TextUtils.isEmpty(name)) {
                if (nameStr.contains(name)) {
                    mListRiverChief.add(dateBean);
                }
            }
            if (!TextUtils.isEmpty(codeStr) && !TextUtils.isEmpty(name)) {
                if (nameStr.contains(name) && codeStr.equals(addvcdarea)) {
                    mListRiverChief.add(dateBean);
                }
            }
            if (!TextUtils.isEmpty(codeStr) && TextUtils.isEmpty(name)) {
                if (codeStr.equals(addvcdarea)) {
                    mListRiverChief.add(dateBean);
                }
            }
        }
        return mListRiverChief;
    }

    public List<RiverDutyBean.DateBean> serachDuty(List<RiverDutyBean.DateBean> mList, String name) {
        List<RiverDutyBean.DateBean> mListRiverChief = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            RiverDutyBean.DateBean dateBean = mList.get(i);
            if (dateBean.getNAME().contains(name)) {
                mListRiverChief.add(dateBean);
            }
        }
        return mListRiverChief;
    }
}
